package cn.itsource.pethome.system.service.impl;

import cn.itsource.pethome.base.query.Page;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.system.domain.Menu;
import cn.itsource.pethome.system.domain.Role;
import cn.itsource.pethome.system.mapper.RoleMapper;
import cn.itsource.pethome.system.service.IRoleService;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-10-19
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void insert(Role role) {//想通透
        if(StringUtils.isEmpty(role)){
            throw new BusinessException("角色编码不能为空");
        }
        Role role1 = roleMapper.querySn(role.getSn());
        if(role1 != null){
            throw new BusinessException("角色编码已存在");
        }
        //保存角色信息
        roleMapper.insert(role);
        Long roleId = role.getId();

        //保存角色的菜单
        roleMapper.insertRoleAndMenu(roleId,role.getMenus());
        //保存角色的权限
        roleMapper.insertRoleAndPermission(roleId,role.getPermissions());
    }
    //  数据库 sn 1
    //  sn 2
    @Override
    @Transactional
    public void update(Role role) {
        if(StringUtils.isEmpty(role)){
            throw new BusinessException("角色编码不能为空");
        }
        //如果 你修改sn 和数据查询的一样？
        //逻辑有点复杂
        Role role1 = roleMapper.querySn(role.getSn());
        //有 没有
        if(role1 != null && role1.getId() != role.getId()){
            throw new BusinessException("角色编码已经存在了");
        }

        //保存角色的菜单
        roleMapper.deleteRoleAndMenu(role.getId());
        //保存角色的菜单
        roleMapper.deleteRoleAndPermission(role.getId());

        roleMapper.update(role);
        //保存角色的菜单
        roleMapper.insertRoleAndMenu(role.getId(),role.getMenus());
        //保存角色的菜单
        roleMapper.insertRoleAndPermission(role.getId(),role.getPermissions());
    }

    @Override
    public PageList<Role> pageQueryAll(Page query) {
        PageList<Role> result = new PageList<>();
        result.setTotal(roleMapper.count(query));
        List<Role> roles = roleMapper.pageQueryAll(query);
        for (Role role : roles) {
            Long roleId = role.getId();
            List<Menu>  mids =roleMapper.queryMenuByRoleId(roleId);
            //处理 mids 将对象变成二维数组
            Long[][] ids = new Long[mids.size()][2];
            for (int i = 0; i < mids.size(); i++) {
                Long[] id = new Long[2];
                Menu menu = mids.get(i);
                id[0] = menu.getParentId();
                id[1] = menu.getId();
                ids[i] = id;
            }
            role.setIds(ids);
            List<Long>  pids =  roleMapper.queryPermissionByRoleId(roleId);
            role.setPermissions(pids);
        }
        result.setData(roles);
        return result;
    }
}
